package cn.fastoo.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class SmsSender {

	private static final Logger log = LoggerFactory.getLogger(SmsSender.class);

	public static final String CAPTCHA_ACCOUNT = "efjtrv"; // 验证码
	public static final String CAPTCHA_ACCOUNTPWD = "klGlgNmQ";
	public static String FASTOO_API_URL;

	/******************发送验证码短信********************/
	public static String sendCaptchSms(String tel, String content) {
		String cpid = CAPTCHA_ACCOUNT;
		String cppwd = CAPTCHA_ACCOUNTPWD;
		return synchronize(tel,content,cpid,cppwd);
	}


	public static String synchronize(String tel, String content,String cpid,String cppwd) {
		try {
			StringBuilder sb = new StringBuilder("http://api2.santo.cc/submit?command=MT_REQUEST");
			sb.append("&cpid=").append(cpid).append("&cppwd=").append(cppwd).append("&da=")
					.append(tel).append("&sm=").append(URLEncoder.encode(content,"UTF-8"));
			HttpClient client = HttpClientHelper.getHttpClient();
			URI uri = new URI(sb.toString());
			log.info("request=" + sb.toString());
			HttpGet httpPost = new HttpGet(uri);
			HttpResponse httpResponse = client.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();
			String res = EntityUtils.toString(entity);
			EntityUtils.consume(entity);
			return res;
		} catch (Exception e) {
			log.error("发送短信异常", e);
			return null;
		}
	}

	public static Map<String, String> doResult(String result) {
		log.info("response result = " + result);
		Map<String, String> map = new HashMap<String, String>();
		// 返回结果为‘000’ 发送成功 mtstat= REJECTD&mterrcode=错误代码
		if ("".equals(result)||result==null) {
			return map;
		}
		String mterrcode = result.substring(result.indexOf("mterrcode=") + 10, result.length());
		String mtmsgid = "";
		if (result.indexOf("mtmsgid=") > -1) {
			mtmsgid = result.substring(result.indexOf("mtmsgid=") + 8, result.indexOf("&mtstat="));
		}
		map.put("mterrcode", mterrcode);
		map.put("mtmsgid", mtmsgid);
		return map;
	}

	public static String fastooApiSend(String fastooApiUrl, String tel, String content,String apiKey) {
		try {
			StringBuilder sb = new StringBuilder(fastooApiUrl+"?apikey=").append(apiKey);
			sb.append("&da=").append(tel).append("&msg=").append(URLEncoder.encode(content,"UTF-8"));
			HttpClient client = HttpClientHelper.getHttpClient();
			URI uri = new URI(sb.toString());
			log.info("request=" + sb.toString());
			HttpGet httpPost = new HttpGet(uri);
			HttpResponse httpResponse = client.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();
			String res = EntityUtils.toString(entity);
			EntityUtils.consume(entity);
			return res;
		} catch (Exception e) {
			log.error("发送短信异常", e);
			return null;
		}
	}

}
